package com.meishij.activity;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.meishij.R;
import com.meishij.adapter.City_Loc_Adapter;
import com.meishij.customerview.GuoLoadDialog;
import com.meishij.customerview.ShiHua_Quick_index_View;
import com.meishij.entry.CityEntry;
import com.meishij.utils.PinYinUtil;

import org.w3c.dom.Text;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static android.util.Log.e;

public class ShiHua_Loc_City_Activity extends AppCompatActivity {

    private List<CityEntry.ChildrenBean> childCityList=new ArrayList<>();
    private ListView city_recycler;
    private TextView tv_current;
    private ShiHua_Quick_index_View quick_index_view;

    private Handler handler = new Handler();
    private GuoLoadDialog loadingDialogFirst;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shi_hua_loc_city);
        new Thread(){
                //子线程读取数据加载
                @Override
                public void run() {
                    InputStream inputStream = getResources().openRawResource(R.raw.city);
                    Type type = new TypeToken<List<CityEntry>>() {
                    }.getType();
                    Object o = null;
                    try {
                        o = new Gson().fromJson(new String(toByteArray(inputStream)), type);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    List<CityEntry> cityEntryList = (List<CityEntry>) o;
                    zhuanhuanCityList(cityEntryList);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //子线程更新UI
                            loadingDialogFirst.dismiss();
                            city_recycler.setAdapter(new City_Loc_Adapter(ShiHua_Loc_City_Activity.this, childCityList));
                        }
                    });
                }
            }.start();
        city_recycler = ((ListView) findViewById(R.id.lv_shihua_quick_city));
        tv_current = ((TextView) findViewById(R.id.tv_shihua_qucik_currentWord));
        quick_index_view = ((ShiHua_Quick_index_View) findViewById(R.id.quick_index));
        quick_index_view.setLetterListener(new ShiHua_Quick_index_View.GetLetterListener() {
            @Override
            public void getLetter(String text) {
                int i = 0;
                int nearestId = 0;
                for (i = 0; i < childCityList.size(); i++) {
                    String firstWord = childCityList.get(i).getPinyin().charAt(0) + "";
                    if (text.equals(firstWord)) {
                        //说明找到了，那么应该讲当前的item放到屏幕顶端
                        city_recycler.setSelection(i);
                        break;//只需要找到第一个就行
                    }
                }
                showCurrentLetter(text);
            }
        });
        loadingDialogFirst = new GuoLoadDialog(this, R.style.MyDialog);
        loadingDialogFirst.setCanceledOnTouchOutside(false);
        loadingDialogFirst.show();
    }

    private void zhuanhuanCityList(List<CityEntry> cityEntryList) {
        for (CityEntry cityEntry : cityEntryList) {
            childCityList.addAll(cityEntry.getChildren());
        }
        Collections.sort(childCityList, new Comparator<CityEntry.ChildrenBean>() {
            @Override
            public int compare(CityEntry.ChildrenBean o1, CityEntry.ChildrenBean o2) {
                o1.setPinyin(PinYinUtil.getPinyin(o1.getText()));
                o2.setPinyin(PinYinUtil.getPinyin(o2.getText()));
                return o1.getPinyin().compareTo(o2.getPinyin());
            }
        });
    }

    private void showCurrentLetter(String letter) {
        tv_current.setVisibility(View.VISIBLE);
        tv_current.setText(letter);
        handler.removeCallbacksAndMessages(null);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                tv_current.setVisibility(View.INVISIBLE);
            }
        }, 500);
    }

    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int len = 0;
        while (-1 != (len = input.read(buffer))) {
            output.write(buffer, 0, len);
        }
        return output.toByteArray();
    }
}
